package gateway.server;

import gateway.api.engine.Engine;
import gateway.config.Config;
import gateway.server.engine.RouteEngine;

import java.util.concurrent.CountDownLatch;

/**
 * @author Jin Zheng
 * @since 1.0 2020-09-10
 */
public class GatewayMain
{
	public GatewayMain()
	{
	}

	public static void main(String[] args) throws Exception
	{
		Config config = Config.loadFromYaml();
		Engine engine = new RouteEngine(config);
		CountDownLatch latch = new CountDownLatch(1);
		Runtime.getRuntime().addShutdownHook(new Thread(() -> destroy(engine, latch)));
		engine.initialize();
		//latch.await();
	}

	private static void destroy(Engine engine, CountDownLatch latch)
	{
		try
		{
			engine.destroy();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			latch.countDown();
		}
	}

}
